package org.wzl.todolisthelper.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.MDC;

@Data
@AllArgsConstructor
public class Resp<T> {

    private String trace;
    private String msg;
    private T data;
    private Integer code;

    public static <T> Resp<T> ok(T data) {
        return new Resp<>(getTraceId(), "success", data, 200);
    }

    public static <T> Resp<T> ok() {
        return new Resp<T>(getTraceId(), "success", null, 200);
    }

    public static <T> Resp<T> fail(String msg) {
        return new Resp<T>(getTraceId(), msg, null, 500);
    }

    public static <T> Resp<T> fail(String msg, Integer code) {
        return new Resp<T>(getTraceId(), msg, null, code);
    }

    public static <T> Resp<T> fail(Integer code, String msg) {
        return new Resp<T>(getTraceId(), msg, null, code);
    }

    private static String getTraceId() {
        return MDC.get("traceId");
    }
}
